package com.im.service.purchasePlan.smo;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.im.service.dto.*;
import com.im.service.purchasePlan.dao.IPurchasePlanQueryDAO;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.im.util.JsonUtil;
import com.im.util.ReturnsUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 * 采购计划查询 SMO
 * @author wangxh
 * 018年3月27日
 */
@Service
public class PurchasePlanQuerySMOImpl implements IPurchasePlanQuerySMO {

  private static final Logger logger = LogManager.getLogger(PurchasePlanQuerySMOImpl.class);

  @Autowired
  private IPurchasePlanQueryDAO purchasePlanQueryDAO;


  @Override
  public PageQuery<PurchasePlan> queryPurchasePlanList(String inJson,Map<String,Object> reqMap) throws Exception  {

    try {
      if (null!=inJson &&!JsonUtil.jsonValidate(inJson)) {
        throw new BaseException(
                new StringBuffer().append("字符串： ").append(inJson).append("不符合JSON格式").toString());
      }
      PageQuery<PurchasePlan> page = JsonUtil.jsonToBean(inJson, PageQuery.class);
      PurchasePlan purchasePlan = JsonUtil.jsonToBean(inJson, PurchasePlan.class);

      Map<String,Object> param = new HashMap<String,Object>();
      param.put("purchasePlan", purchasePlan);
      param.put("page", page);
      //特殊参数处理
      if(null!=reqMap){
        if(null!=reqMap.get("impleProjIds")){
          param.put("impleProjIds",reqMap.get("impleProjIds"));
        }
      }

      PageHelper.startPage(page.getPageNumber(), page.getPageSize());
      List<PurchasePlan> list=purchasePlanQueryDAO.queryPurchasePlanList(param);
      page.setRows(list);
      page.setTotalPages(((Page<PurchasePlan>)list).getPages());
      page.setTotal(((Page<PurchasePlan>) list).getTotal());
      return page;
    } catch (Exception e) {
      if (logger.isErrorEnabled()) {
        logger.error(ReturnsUtil.getStackTrackMessage(e));
      }
      throw new Exception(ReturnsUtil.getStackTrackMessage(e));
    }
  }

  @Override
  public PageQuery<PurchasePlan> queryPurchasePlanListRelated(String inJson,Map<String,Object> reqMap) throws Exception {
    try {
      PageQuery<PurchasePlan> page = JsonUtil.jsonToBean(inJson, PageQuery.class);
      Map<String,Object> param = new HashMap<String,Object>();
      param.put("page", page);


      //特殊参数处理
      if(null!=reqMap){
        if(null!=reqMap.get("orgId")){
          param.put("orgId",reqMap.get("orgId"));
        }
        if(null!=reqMap.get("planProjClass")){
          param.put("planProjClass",reqMap.get("planProjClass"));
        }
        if(null!=reqMap.get("impleProjId")){
          param.put("impleProjId",reqMap.get("impleProjId"));
        }
        if(null!=reqMap.get("startDt")){
          param.put("startDt",reqMap.get("startDt"));
        }
        if(null!=reqMap.get("endDt")){
          param.put("endDt",reqMap.get("endDt"));
        }
        if(null!=reqMap.get("statusCd")){
            param.put("statusCd",reqMap.get("statusCd"));
          }

      }

      PageHelper.startPage(page.getPageNumber(), page.getPageSize());
      List<PurchasePlan> list=purchasePlanQueryDAO.queryPurchasePlanListRelated(param);
      if (list != null && list.size() > 0) {
        //附件
        for (PurchasePlan purchasePlan : list) {
          Map<String, Object> filesParam = new HashMap<String, Object>();
          filesParam.put("uploadObjId",purchasePlan.getPurchPlanId() );
          filesParam.put("uploadObjType", "220");
          List<FileUpload> fileUploads = purchasePlanQueryDAO.queryFileUploadInfos(filesParam);
          if (fileUploads.size() > 0) {
            purchasePlan.setFileUploads(fileUploads);
          }

        }
      }

      page.setRows(list);
      page.setTotalPages(((Page<PurchasePlan>)list).getPages());
      page.setTotal(((Page<PurchasePlan>) list).getTotal());
      return page;
    } catch (Exception e) {
      if (logger.isErrorEnabled()) {
        logger.error(ReturnsUtil.getStackTrackMessage(e));
      }
      throw new Exception(ReturnsUtil.getStackTrackMessage(e));
    }
  }

  @Override
  public PurchasePlan queryPurchasePlanDetail(PurchasePlan req) throws Exception{

    try {
      Map<String,Object> param = new HashMap<String,Object>();
      param.put("purchasePlan", req);
      PurchasePlan purchasePlanDetail=purchasePlanQueryDAO.queryPurchasePlanDetail(param);
      return purchasePlanDetail;
    } catch (Exception e) {
      if (logger.isErrorEnabled()) {
        logger.error(ReturnsUtil.getStackTrackMessage(e));
      }
      throw new Exception(ReturnsUtil.getStackTrackMessage(e));
    }
  }
  
  
  public List<PurchasePlan> queryPurchasePlanByParam(Map<String,Object> reqMap) throws Exception{
	  return purchasePlanQueryDAO.queryPurchasePlanByParam(reqMap);
  }
}
